package com.woniu.zsd.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.order.config.MyWebSocketHandler;
import com.woniu.zsd.order.entity.OrderInfo;
import com.woniu.zsd.order.from.*;
import com.woniu.zsd.order.service.IOrderInfoService;
import com.woniu.zsd.order.util.OssUtil;
import com.woniu.zsd.order.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@RestController
@RequestMapping("/order-info")
@Api(tags = "订单详情")
public class OrderInfoController {

    @Resource
    private IOrderInfoService iOrderInfoService;
    @Autowired
    private TokenUtil tokenUtil;


    @Resource
    private OssUtil ossUtil;


    /**
     * 用户查询自己的订单可以通过订单创建时间查询，通过站点id查询
     *
     * @param fof
     * @return
     */
    @ApiOperation("用户条件查询自己的订单")
    @GetMapping("/findUserOrder")
    public Result<Page<OrderInfo>> findUserOrder(FindUserOrderFrom fof,@RequestHeader String token) {
        System.out.println(token);
        Integer userId = tokenUtil.getAdminId(token);
        System.out.println(userId);
        Page<OrderInfo> orderDto = iOrderInfoService.findUserOrder(fof,userId);
        return new  Result<Page<OrderInfo>>().setData(orderDto).setCode(200);
    }

    /**
     * 商家查询所有订单，可以通过用户手机号查询，临时工姓名查询，订单状态查询，入库时间查询，站点id查询
     */
    @ApiOperation("商家条件查询所有订单")
    @GetMapping("/findAllOrder")
    public Result<Page<OrderInfo>> findAllOrder(FindAllOrderFrom faf) {
        Page<OrderInfo> orderInfoIPage = iOrderInfoService.findAllOrder(faf);
        return new  Result<Page<OrderInfo>>().setData(orderInfoIPage).setCode(200);
    }

    /**
     * 临时工查询自己负责的订单，通过订单状态查询，根据用户预约时间查询
     */
    @ApiOperation("临时工条件查询订单")
    @GetMapping("/findWorkOrder")
    public Result<Page<OrderInfo>> findWorkOrder(FindWorkOrderFrom fwf,@RequestHeader String token) {
        Integer id = tokenUtil.getWorkId(token);
        Page<OrderInfo> infoIPage = iOrderInfoService.findWorkOrder(fwf,id);
        return new  Result<Page<OrderInfo>>().setData(infoIPage).setCode(200);
    }


    /**
     * 用户上传废品图片和废品信息描述
     */
    @ApiOperation("上传废品图片(用户回收人员)")
    @PostMapping("/upWasteImage")
    public Result<String> upWasteImage(@RequestPart("file") MultipartFile file){
        String s = ossUtil.uploadFile(file);
        return Result.getResult(String.class).setMsg("上传成功").setData(s);
    }

    /**
     * 用户创建订单
     */
    @ApiOperation("用户创建订单")
    @PostMapping(value = "/creatOrder")
    public Result userCreatOrder(@RequestBody UserCreatOrderFrom ucf,@RequestHeader String token) throws Exception {
        Integer id = tokenUtil.getAdminId(token);
        iOrderInfoService.userCreatOrder(ucf,id);
        return Result.getResult().setMsg("下单成功,请查看该订单是否给您分配了附近的站点，如果没有请联系管理员");
    }
    @ApiOperation("取消订单方法")
    @PutMapping("/cancelOrder")
    public Result cancelOrder(@RequestBody CancelOrderFrom cof) throws Exception {
        iOrderInfoService.cancelOrder(cof);
        return Result.getResult().setMsg("取消成功");
    }

    /**用户最终确认订单
     * @return
     * @throws Exception
     */
    @ApiOperation("用户确认订单")
    @PutMapping("/confirmOrder")
    private Result confirmOrder(@RequestParam("orderNumber") String orderNumber,@RequestHeader String token) throws Exception {
        Integer id = tokenUtil.getAdminId(token);
        iOrderInfoService.confirmOrder(orderNumber,id);
        return Result.getResult().setMsg("确认完成");
    }

    /**
     * 站点分配回收人员上门回收
     */
    @ApiOperation("站点分配订单")
    @PutMapping("/siteAllocationOrder")
    public Result siteAllocationOrder(@RequestBody SiteAllocationOrderFrom sof) throws Exception {
        iOrderInfoService.siteAllocationOrder(sof);
        return Result.getResult().setMsg("分配成功");
    }

    /**
     * 站点分配给回收人员，回收人员选择接取这个订单
     * @param orderNum
     * @param token
     * @return
     */
    @ApiOperation("回收工作人员接取订单")
    @PutMapping("/workUpdateOrder")
    public Result workUpdateOrder(@ApiParam("订单编号") @RequestParam("orderNum")String orderNum, @RequestHeader String token) throws Exception {
        Integer id = tokenUtil.getWorkId(token);
        iOrderInfoService.workUpdateOrderStatus(orderNum,id);
        return Result.getResult().setMsg("接取成功");
    }

    /**
     * 回收人员回收完成站点完成入库
     * @param orderNumber
     * @return
     * @throws Exception
     */
    @ApiOperation("站点确认废品入库")
    @PutMapping("/siteUpdateOrder")
    public Result siteUpdateOrder(@RequestParam("orderNumber") String orderNumber ) throws Exception {
        iOrderInfoService.siteUpdateOrder(orderNumber);
        return Result.getResult().setMsg("确认成功");
    }

    @ApiOperation("工作人员拒绝接单")
    @PutMapping("/workNoOrder")
    public Result workNoOrder(@RequestParam("orderNUmber") String orderNumber){
        iOrderInfoService.workNoOrder(orderNumber);
        return Result.getResult().setMsg("拒绝成功");
    }


    @PutMapping("/adminUpdateOrder")
    @ApiOperation("商家分配站点处理订单")
    public Result adminUpdateOrder(@RequestBody AdminUpdateOrder auo) throws IOException {
        iOrderInfoService.adminUpdateOrder(auo);
        return Result.getResult().setMsg("分配成功");
    }

    /**
     * 查看用户是否有完成的订单
     */
    @GetMapping("/isOrderFinish")
    @Operation(summary = "查看用户是否有完成的订单")
    public boolean isOrderFinish( Integer userId,Integer siteId){
        System.out.println("userId:"+userId+"siteId:"+siteId);
        return iOrderInfoService.isOrderFinish(userId,siteId);
    }










}
